

function(set_arduino_platform platform_in_)
  string(TOUPPER "${platform_in_}" platform_)
  # reinclusion notification:
  if(ARDUINO_PLATFORM)
    if( ARDUINO_PLATFORM STREQUAL platform_ )
#      message(AUTHOR_WARNING "Attempting to call arduino_platform when ARDUINO_PLATFORM is already set.")
#      return()
    else()
      message(FATAL_ERROR "Attempting to call arduino_platform when ARDUINO_PLATFORM is already set AND ${platform_} != ${ARDUINO_PLATFORM}")
    endif()
  else()
    set(ARDUINO_PLATFORM ${platform_} )
    set(ARDUINO_PLATFORM ${platform_} PARENT_SCOPE)
  endif()

  # test to see if name exists...
  if( NOT ARDUINO_${platform_}_NAME )
    show_arduino_platforms()
    message(FATAL_ERROR "${platform_in_} doesn't seem to be a valid arduino platform.")
  endif()

  #
  # Set the build flags
  #
  if( ARDUINO_${ARDUINO_PLATFORM}_BUILD_CORE STREQUAL "arduino" )
    # locally
    set(CMAKE_SYSTEM_NAME Generic)
    set(CMAKE_C_COMPILER avr-gcc)
    set(CMAKE_CXX_COMPILER avr-g++)
    set(CMAKE_AR avr-ar)
    set(CMAKE_RANLIB avr-ranlib)
    set(CMAKE_)
    set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
    # C only fine tunning
    set(TUNNING_FLAGS "-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums")

    set(CMAKE_CXX_FLAGS "-mmcu=${ARDUINO_${platform_}_BUILD_MCU} -DF_CPU=${ARDUINO_${platform_}_BUILD_F_CPU} -Os")
    set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} ${TUNNING_FLAGS} -Wall -Wstrict-prototypes -std=gnu99")

    # include the 1.0 variant include diorectory
    if(ARDUINO_${platform_}_BUILD_VARIANT)
      FIND_PATH(ARDUINO_VARIANTS_DIR pins_arduino.h
        PATHS
          ${ARDUINO_VARIANTS_ROOT_DIR}/${ARDUINO_${platform_}_BUILD_VARIANT}
        )
      if( NOT ARDUINO_VARIANTS_DIR )
        message( "${ARDUINO_VARIANTS_ROOT_DIR}" )
        message(FATAL_ERROR "Couldn't find include directory for variant: ${ARDUINO_${platform_}_BUILD_VARIANT}" )
      endif()
      set(ARDUINO_INCLUDE_DIR ${ARDUINO_INCLUDE_DIR} ${ARDUINO_VARIANTS_DIR})
    endif()

  elseif( ARDUINO_${ARDUINO_PLATFORM}_BUILD_CORE STREQUAL "pic32" )
    message(FATAL_ERROR "pic32/chipkit not implemented, yet...")
  endif()

  # Now in parent scope...
  set(CMAKE_SYSTEM_NAME ${CMAKE_SYSTEM_NAME} PARENT_SCOPE)
  set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER} PARENT_SCOPE)
  set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER} PARENT_SCOPE)
  set(CMAKE_AR ${CMAKE_AR} PARENT_SCOPE)
  set(CMAKE_RANLIB ${CMAKE_RANLIB} PARENT_SCOPE)
  set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} PARENT_SCOPE)
  set(TUNNING_FLAGS ${TUNNING_FLAGS} PARENT_SCOPE)
  set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} PARENT_SCOPE)
  set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} PARENT_SCOPE)

  message(STATUS "Setting Arduino Platform to ${platform_}")
  add_arduino_core()
  foreach(lib_ ${Arduino_FIND_COMPONENTS})
    add_arduino_component(${lib_})
  endforeach()

  set(ARDUINO_LIBS ${ARDUINO_LIBS} PARENT_SCOPE)
  set(ARDUINO_INCLUDE_DIR ${ARDUINO_INCLUDE_DIR} PARENT_SCOPE)

endfunction()
